#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Highlight the placeholders with some nice borders.

This will place the placeholder content in the following template:

<div class="placeholder_marker_container placeholder_marker_level_0">
	<div class="placeholder_marker_header">
		<span class="placeholder_marker_url"> (url) </span>
		<span class="placeholder_marker_name"> (name) </span>
	</div>
	<div class="placeholder_marker_content">
		(placeholder content)
	</div>
</div>
"""

import xml.dom.minidom

def execute_minidom(server, document, parameters):
	mark_placeholders(server, document)

	head = document.getElementsByTagName('head') [0]

	style = document.createElement('style')
	style.setAttribute('type', 'text/css')

	style.appendChild(document.createTextNode(
"""
* { color: #000000; }

div.placeholder_marker_level_0 { background-color: #44ff44; border: 1px solid #008800; }
div.placeholder_marker_level_0 div.placeholder_marker_header { background-color: #008800; }
div.placeholder_marker_level_0 div.placeholder_marker_header * { color: #aaffaa; }

div.placeholder_marker_level_1 { background-color: #ff4444; border: 1px solid #880000; }
div.placeholder_marker_level_1 div.placeholder_marker_header { background-color: #880000; }
div.placeholder_marker_level_1 div.placeholder_marker_header * { color: #ffaaaa; }

div.placeholder_marker_level_2 { background-color: #4444ff; border: 1px solid #000088; }
div.placeholder_marker_level_2 div.placeholder_marker_header { background-color: #000088; }
div.placeholder_marker_level_2 div.placeholder_marker_header * { color: #aaaaff; }

div.placeholder_marker_level_3 { background-color: #ff44ff; border: 1px solid #880088; }
div.placeholder_marker_level_3 div.placeholder_marker_header { background-color: #880088; }
div.placeholder_marker_level_3 div.placeholder_marker_header * { color: #ffaaff; }

div.placeholder_marker_level_4 { background-color: #ffff44; border: 1px solid #888800; }
div.placeholder_marker_level_4 div.placeholder_marker_header { background-color: #888800; }
div.placeholder_marker_level_4 div.placeholder_marker_header * { color: #ffffaa; }

div.placeholder_marker_level_5 { background-color: #44ffff; border: 1px solid #008888; }
div.placeholder_marker_level_5 div.placeholder_marker_header { background-color: #008888; }
div.placeholder_marker_level_5 div.placeholder_marker_header * { color: #aaffff; }

div.placeholder_marker_content { padding: 2px 8px; }
"""))

	head.appendChild(style)

def mark_placeholders(server, document, current_node=None, level=0):
	if not current_node:
		current_node = document.getElementsByTagName('html') [0]

	level = level % 6;

	# Don't modify a collection collection we're walking through
	# Cache the nodes we need to process
	nodes_to_process = [ ]

	# Look for nodes looking like <pp:placeholder name="..." url-base="...">
	for n in current_node.childNodes:
		if n.nodeType == document.ELEMENT_NODE:
			if n.localName == 'placeholder' and n.prefix == 'pp':
				nodes_to_process.append(n)
				mark_placeholders(server, document, n, level + 1)
			else:
				mark_placeholders(server, document, n, level)

	# Now insert comments
	for n in nodes_to_process:
		# Create elements
		container = document.createElement('div')
		container.setAttribute('class', 'placeholder_marker_container placeholder_marker_level_%s' % level)

		marker_header = document.createElement('div')
		marker_header.setAttribute('class', 'placeholder_marker_header')

		marker_link = document.createElement('a')
		marker_link.setAttribute('href', '/python-pages/administration/view-page/%s' % n.getAttribute('url-base'))
		marker_link.setAttribute('title', 'View page source code')

		marker_url = document.createElement('span')
		marker_url.setAttribute('class', 'placeholder_marker_url')
		marker_url.appendChild(document.createTextNode(n.getAttribute('url-base')))

		marker_name = document.createElement('span')
		marker_name.setAttribute('class', 'placeholder_marker_name')
		marker_name.appendChild(document.createTextNode(' (%s)' % n.getAttribute('name')))

		marker_content = document.createElement('div')
		marker_content.setAttribute('class', 'placeholder_marker_content')

		marker_link.appendChild(marker_url)
		marker_link.appendChild(marker_name)
		marker_header.appendChild(marker_link)

		container.appendChild(marker_header)
		container.appendChild(marker_content)

		# Add childs to content node
		while n.firstChild:
			marker_content.appendChild(n.firstChild)

		# Add container to original node
		n.appendChild(container)
